package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.mapper.NewsMapper;
import com.example.demo.pojo.News;
import com.example.demo.utils.LayuiResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/end/news")
public class EndNewsController {

    @Resource
    private NewsMapper newsMapper;


    @RequestMapping("/add")
    public Object add(@RequestBody News news){
        news.setNewsCreateTime(new Date());
        newsMapper.insert(news);
        return LayuiResult.addSuccess();
    }

    @RequestMapping("/delete/{id}")
    public Object delete(@PathVariable Integer id){
        News news = new News();
        news.setId(id);
        news.setStatus(0);
        newsMapper.updateById(news);
        return LayuiResult.deleteSuccess();
    }

    @RequestMapping("/update")
    public Object update(@RequestBody News news){
        newsMapper.updateById(news);
        return LayuiResult.updateSuccess();
    }

    @RequestMapping("/get")
    public Object get(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        List<News> news = newsMapper.selectList(queryWrapper);
        Integer count = newsMapper.selectCount(queryWrapper);
        PageInfo<News> pageInfo = new PageInfo<>(news);
        return LayuiResult.selectSuccess(pageInfo.getList(),count);
    }

    @RequestMapping("/search/{newsTitle}")
    public Object search(@PathVariable String newsTitle, Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1).like("news_title",newsTitle);
        List<News> news = newsMapper.selectList(queryWrapper);
        Integer count = newsMapper.selectCount(queryWrapper);
        PageInfo<News> pageInfo = new PageInfo<>(news);
        return LayuiResult.selectSuccess(pageInfo.getList(),count);
    }

}
